<?php

/**
 * Description:
 * @Author: Tudo<997470395@qq.com>
 * @Created: 2015-3-31 下午2:41:05
 */
namespace Home\Controller;

class LoginController extends BaseController
{
    public function __construct() {
        parent::__construct();
        $this->assign("choose", I("get.choose", ""));
    }

    public function index() {
        if (IS_POST) {
            $map['email'] = I('post.username', '', "trim");
            $map['password'] = I('post.passwd', '', "trim");
            $uid = D("User")->checkLogin($map['password'], $map['email']);
            if ($uid < 0) {
                $this->error("登陆失败！");
            }

            $this->initLogin($uid);
            $this->success("登陆成功！", U("index/index"));
            return 0;
        }
        $this->display("login");
    }

    public function check() {
        $name = I("post.name", "");
        $value = I("post.param", "");
        switch ($name) {
            case "mobile":
                $user = D("User");
                if ($user->hasMobile($value)) {
                    $array['info'] = "手机号已经存在!";
                    $array['status'] = "n";
                } else {
                    $array['info'] = "";
                    $array['status'] = "y";
                }
        }
        $this->ajaxReturn($array, "Json");
    }

    public function register() {
        if (IS_POST) {
            $verfiy = I("post.verify", 0);
            if (!A("Verify")->verify($verfiy)) {
                $this->error("验证码不正确");
            }
            $user = D("User");
            $data = I("post.");
            if (!$user->create($data)) {
                $this->error($user->getError());
            }
            $uid = $user->addUser();
            if (!$uid) {
                $this->error("注册失败");
            }
            $this->initLogin($uid);
            $this->success("注册成功！", U('index/index'));
            return;
        }
        $choose = I("get.choose", "");
        if (empty($choose) || !in_array($choose, array("job", "business"))) {
            redirect(U("/home/choose"));

        }
        $this->assign($this->outPut);
        $this->display();
    }

    public function exits() {
        cookie('u_user', null);
        $this->success("退出成功", U('index/index'));
    }

    public function choose() {
        $this->display();
    }
}